package com.pig4cloud.pig.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.Service.EducationService;
import com.pig4cloud.pig.demo.Util.TokenUtils;
import com.pig4cloud.pig.demo.feign.Entity.Education;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 学历认证-控制器
 * Created by caoxuel on 2020/10/27 10:10
 * @author caoxuel
 */
@Inner
@RestController
@RequiredArgsConstructor
@RequestMapping("/article/education")
public class EducationController {

    @Autowired
    private EducationService educationService;


    /**
     * 学历认证-设置学历信息或修改学历信息
     * @param education
     * @return
     */
    @Inner(value = false)
    @PostMapping("/set")
    public R setEducation(@RequestBody Education education) {
        //如果没有主键，则为新增；如果有主键，则为修改
        if (education.getEduId() != null) {
            educationService.updateById(education);
        } else {
            educationService.save(education);
        }

        return R.ok();
    }

    /**
     * 根据用户id获取学历信息
     */
    @Inner(value = false)
    @GetMapping("/getEducation")
    public R getEducation(HttpServletRequest request) {
        Long id = TokenUtils.getId(request);
        QueryWrapper<Education> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);

        Education education = educationService.getOne(wrapper);
        return R.ok(education);
    }




    //后台管理增删改查

    /**
     * 分页查询
     * @param page 分页对象
     * @param education
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('generator_education_get')" )
    public R getEducationPage(Page page, Education education) {
        return R.ok(educationService.page(page, Wrappers.query(education)));
    }


    /**
     * 通过id查询
     * @param eduId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{eduId}" )
    @PreAuthorize("@pms.hasPermission('generator_education_get')" )
    public R getById(@PathVariable("eduId" ) Long eduId) {
        return R.ok(educationService.getById(eduId));
    }

    /**
     * 新增
     * @param education
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('generator_education_add')" )
    public R save(@RequestBody Education education) {
        return R.ok(educationService.save(education));
    }

    /**
     * 修改
     * @param education
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('generator_education_edit')" )
    public R updateById(@RequestBody Education education) {
        return R.ok(educationService.updateById(education));
    }

    /**
     * 通过id删除
     * @param eduId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{eduId}" )
    @PreAuthorize("@pms.hasPermission('generator_education_del')" )
    public R removeById(@PathVariable Long eduId) {
        return R.ok(educationService.removeById(eduId));
    }

}
